﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Threading;
using System.IO;
using System.Threading;

namespace ShumaDaf
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        // global for GC prorection
        Mutex m = null;
        public App()
        {
            this.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
            checkForMutex();
        }

        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            StreamWriter error = new StreamWriter("Exception log.txt", true);
            error.WriteLine("Новая ошибка {0}", DateTime.Now);
            error.WriteLine("Message: {0}", e.Exception.Message);
            error.WriteLine("StackTrace: {0}", e.Exception.StackTrace);
            error.WriteLine("Source: {0}", e.Exception.Source);
            error.Close();
            System.Windows.MessageBox.Show("Please send text file 'Exception log.txt' from "+Directory.GetCurrentDirectory()+" INSIDE mail body (not attachment) to sherbanov@hotmail.com", "Hellpass - welcome!");
            e.Handled = true;
            this.Shutdown(1);
        }

        void checkForMutex()
        {
            bool requestInitialOwnership = true;
            bool mutexWasCreated;
            m = new Mutex(requestInitialOwnership,
                                "myMutex",
                                out mutexWasCreated);
            GC.KeepAlive(m);

            if (!(mutexWasCreated))
            {
                MessageBox.Show("Another copy of Shumadaf detected. This copy exit now.", "Second copy protection");
                this.Shutdown(1);
            }
            m.WaitOne();
        }
    }
}
